<template>
  <div class="invest-box">
    <div class="popbtm-btnbox" :class="{ 'lower-layer': onShowEditMoneyPopup }" v-if="!showCancelOrderPopup">
      <div class="balance" v-if="['PCNN'].includes(currentGame)">
        <span>冻结余额：</span>
        <span>{{ userInfo.freeze }}</span>
      </div>
      <div class="red-pop">
        <div class="div" @click="openOrderPopup(!showOrderPopup)">
          <span>{{ howMuch }}</span>
          <img v-if="!showOrderPopup" src="@/assets/img/up.png" alt="" />
          <img v-else src="@/assets/img/show.png" alt="" />
        </div>
      </div>
      <!-- :class="isCircle && ''" -->
      <div class="pop-btm circle-btm">
        <div class="btm-btnbox">
          <template v-for="(item, index) in investMoneyOpts">
            <div
              class="li all-center"
              @click="setInvestMoney(index)"
              :key="index"
              v-if="item.money !== ''"
              :class="{
                activeInvestMoney: Number(inputMoney) === Number(item.money) && index !== 0,
              }"
            >
              <img :src="item.img" v-if="item.img" alt="" />
              <span v-else>{{ item.money }}</span>
            </div>
          </template>
        </div>
        <div class="text all-center">
          <Field
            v-model="oninputMoney"
            @change="updateInputMoney(oninputMoney)"
            input-align="center"
            :maxlength="moneyLength"
            type="digit"
            placeholder="输入金额"
          />
        </div>
      </div>
      <div class="btm-btn all-center row-flex">
        <div class="btn-box">
          <div
            class="cancel-btn btn-bg"
            :class="{ active: investStore?.historyData?.records?.length > 0 }"
            @click="openCancelOrderPopup(true)"
          >
            <i class="icon cancel" />
            <span>撤单</span>
          </div>

          <div
            class="clear-btn btn-bg"
            :class="{ active: investStore?.investList?.length > 0 }"
            @click="clearAllInvestList"
          >
            <i class="icon clear" />
            <span>清空</span>
          </div>

          <template v-if="Object.prototype.hasOwnProperty.call(slots, 'betBtn')">
            <slot name="betBtn" />
          </template>

          <template v-else>
            <div class="bet-btn" @click="betNow">
              <div class="main-text">{{ getInvestBtnText.text }}</div>
              <div v-if="investList.length > 0">{{ howMuch }}</div>
            </div>
          </template>
        </div>
      </div>
    </div>

    <Popup
      v-model:show="onShowEditMoneyPopup"
      @click-overlay="closeOverlay"
      position="bottom"
      :round="true"
      :style="{ height: '163px' }"
      class="edit-popup"
    >
      <div class="popups">
        <div class="popup-top">
          <div class="popup-top-left"></div>
          <div class="pop-tailtext">编辑金额</div>
          <div class="popup-top-right">
            <Icon size="20" color="white" name="arrow-down" @click="onShowEditMoneyPopup = false" />
          </div>
        </div>

        <div class="popups-btnbox">
          <div class="popups-btn">
            <CellGroup>
              <Field
                v-for="(item, index) in getEditInvestMoneyOpts"
                @change="editMoney({ item, index })"
                v-model="item.money"
                type="digit"
                :maxlength="moneyLength"
                :key="index"
                placeholder="请输入..."
              />
            </CellGroup>
          </div>
        </div>
        <div class="btm-btn">
          <div class="btn-clears" @click="initInvestMoney">恢复默认</div>
          <div class="btn-ok" @click="saveMoneyOpts">保存</div>
        </div>
      </div>
    </Popup>

    <!--  确认投注 -->
    <ConfirmBet :disabledInput="disabledInput" v-model:show="showOrderPopup" @delSingle="delSingle" />

    <!-- 撤销投注 -->
    <CancelOrder :chatroomCancelId="chatroomCancelId" @closeCancelOrder="handleCloseCancelOrder" />
  </div>
</template>

<script>
import { computed, useSlots } from 'vue'
import { mapState, mapActions, mapGetters } from 'pinia'
import { useInvestStore } from '@/stores/invest'
import { useUserStore } from '@/stores/user'
import { useGameStore } from '@/stores/game'
import { numberToChinese } from '@/utils'
import ConfirmBet from '@/components/invest/confirmBet.vue'
import CancelOrder from '@/components/invest/cancelOrder.vue'
import { Field, CellGroup, Popup, Icon } from 'vant'

const disableGameSet = new Set(['XGLHC', 'JNDLHC'])
const disabledSubGameSet = new Set(['ZXBZ', 'LX', 'LM'])
export default {
  components: { ConfirmBet, CancelOrder, Field, CellGroup, Popup, Icon },
  props: {
    isCircle: {
      // 快捷金额是否使用圆圈样式 --百家乐和牛牛可用
      type: Boolean,
      default: () => false,
    },
    chatroomCancelId: {
      type: [String, Number],
    },
  },
  data() {
    return {
      moneyLength: 6,
      disabledInput: false, //禁用金额输入框
      showOrderPopup: false, //显示订单弹窗
      investStore: useInvestStore(),
    }
  },

  setup() {
    const userStore = useUserStore()

    const gameStore = useGameStore()

    const userInfo = computed(() => userStore.userInfo)

    const currentGame = computed(() => gameStore.currentGame)

    const currentSubGame = computed(() => gameStore.currentSonGameTag)

    const slots = useSlots()

    return { userInfo, currentGame, slots, currentSubGame }
  },

  computed: {
    ...mapState(useInvestStore, {
      showEditMoneyPopup: (state) => state.showEditMoneyPopup,
      showInvestPopup: (state) => state.showInvestPopup,
      investMoneyIndex: (state) => state.investMoneyIndex,
      investMoneyOpts: (state) => state.investMoneyOpts,
      inputMoney: (state) => state.inputMoney,
      showCancelOrderPopup: (state) => state.showCancelOrderPopup,
      investList: (state) => state.investList,
    }),

    ...mapGetters(useInvestStore, ['getEditInvestMoneyOpts', 'howMuch', 'getInvestCount', 'getInvestBtnText']),

    oninputMoney: {
      get() {
        return this.inputMoney
      },
      set(val) {
        this.updateInputMoney(val)
      },
    },

    onShowEditMoneyPopup: {
      get() {
        return this.showEditMoneyPopup
      },
      set(val) {
        this.updateShowEditMoneyPopup(val)
      },
    },
  },
  methods: {
    ...mapActions(useInvestStore, [
      'saveMoneyOpts',
      'initInvestMoney',
      'editMoney',
      'closeOverlay',
      'setInvestMoney',
      'setInputMoney',
      'showPopup',
      'setInvestNum',
      'openCancelOrderPopup',
    ]),

    numberToChinese,

    // 打开确认投注弹窗
    openOrderPopup(isShow) {
      this.showOrderPopup = isShow
      // 六合彩并且是这几种玩法的，不能自定义每个号码的金额，要统一金额
      this.disabledInput = disableGameSet.has(this.currentGame) && disabledSubGameSet.has(this.currentSubGame)
    },

    // 更新金额
    updateInputMoney(val) {
      this.setInputMoney(val)
    },

    //显示编辑金额弹窗
    updateShowEditMoneyPopup(val) {
      this.showPopup(val)
    },

    // 立即下注
    betNow() {
      this.$emit('betNow')
    },

    delSingle(it) {
      this.$emit('delSingle', it)
    },

    // 清空数据
    clearAllInvestList() {
      this.$emit('updateInvestList', 'del')
      this.setInvestNum([])
    },

    handleCloseCancelOrder() {
      this.$emit('closeCancelOrder')
    },
  },
}
</script>

<style scoped lang="less">
.invest-box {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 102px;
  background: #fff;
  box-shadow: 0 -2px 5px #d1e0ff;
  z-index: 2;

  .lower-layer {
    z-index: 2000 !important;
  }

  .edit-popup {
    background: rgba(28, 32, 53, 1);

    .van-cell-group.van-hairline--top-bottom {
      &::after {
        display: none;
      }
    }
  }

  .popups {
    position: relative;
    box-sizing: border-box;

    .popup-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      padding: 6px 10px 6px;
      background: rgba(102, 91, 225, 1);

      .pop-tp-center {
        width: 50px;
        height: 3px;
        margin-top: 2px;
        border-radius: 2px;
        background: #505e92;
      }

      .popup-top-left {
        flex: 1;
      }

      .popup-top-right {
        flex: 1;
        display: flex;
        justify-content: end;
      }

      .pop-tailtext {
        font-size: 14px;
        color: #fff;
        //text-align: left;
        margin-left: 0;
        flex: 2;
        text-align: center;

        .span1 {
          font-size: 10px;
          color: red;

          .span2 {
            font-size: 12px;
          }
        }
      }
    }

    .popup-tops {
      margin: 0 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 8px 0 10px;
      border-bottom: 1px solid #505e92;

      .pop-tp-centerbtm {
        // width: 50px;
        // height: 3px;
        margin-top: 2px;
        color: #505e92;
        // border-radius: 2px;
        // background: #505E92;
      }

      .pop-tp-right {
        width: 14px;
        height: 8px;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }

    .popups-btnbox {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      margin: 10px;

      .popups-btn {
        display: flex;
        justify-content: center;
        align-items: center;
        margin: 0 3px;

        :deep(.van-cell-group) {
          display: flex;
          justify-content: center;
          align-items: center;
          background-color: transparent;

          .van-cell {
            flex: 1;
            margin: 0 3px;
            border-radius: 5px;
            padding: 0 10px;
            height: 32px;
            line-height: 32px;
            background: linear-gradient(0deg, #2a2c5d, #2a2c5d);
            border: 1px solid rgba(59, 61, 110, 1);

            &::after {
              display: none;
            }

            .van-field__control {
              color: rgba(197, 207, 255, 1);
              text-align: center;
            }
          }
        }
      }
    }

    .btm-btn {
      display: flex;
      justify-content: space-around;
      align-items: center;
      color: #fff;
      margin: 15px 5px 0 0;

      .btn-clears {
        width: 88px;
        height: 30px;
        line-height: 30px;
        background: radial-gradient(113.17% 113.81% at 94.17% 0%, #b7c2cf 0%, #7f92a9 100%)
          /* warning: gradient uses a rotation that is not supported by CSS and may not behave as expected */;
        border-radius: 6px;
      }

      .btn-ok {
        width: 80px;
        height: 30px;
        line-height: 30px;
        background: linear-gradient(180deg, #7758fd 0%, #684dde 100%);
        // background-size: 100% 100%;
        border-radius: 6px;
      }
    }
  }
}

.popbtm-btnbox {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  min-height: 102px;
  z-index: 3000;
  padding-bottom: 11px;
  box-sizing: border-box;
  background: rgba(30, 31, 49, 1);

  .red-pop {
    position: absolute;
    top: -15px;
    z-index: 99;
    left: 0;
    width: 100%;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;

    .div {
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgba(118, 86, 236, 1);
      border-radius: 5px;
      padding: 4px 10px;
      border: 0.2px solid rgba(55, 57, 83, 1);

      span {
        color: #fff;
        font-size: 9px;
      }

      img {
        width: 9.5px;
        height: 5.5px;
        margin-left: 5px;
      }
    }
  }

  .pop-btm {
    position: relative;
    box-sizing: border-box;
    margin-top: 8px;
    padding: 0 8px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    height: 29px;
    width: 100%;

    .btm-btnbox {
      flex: 1;
      border: 1px solid #d7dbe3;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      border-radius: 5px;
      height: 100%;

      .li {
        border-right: 1px solid #d7dbe3;
        flex: 1;
        color: #000;
        height: 100%;

        img {
          width: 18px;
          height: 18px;
        }

        span {
          font-size: 11px;
          line-height: 11px;
        }
      }

      .li:last-child {
        border-right: 0;
        border-top-right-radius: 5px;
        border-bottom-right-radius: 5px;
      }

      .activeInvestMoney {
        background-image: linear-gradient(to bottom, #f78080, #f23a3a);
        color: #fff;
        // border:1px solid #809FF7;
      }
    }

    .text {
      box-sizing: border-box;
      width: 65px;
      height: 100%;
      margin-left: 2px;

      :deep(.van-cell) {
        position: relative;
        display: flex;
        box-sizing: border-box;
        width: 100%;
        text-align: center;
        padding: 2px 5px;
        overflow: hidden;
        font-size: 11px;
        line-height: 24px;
        background-color: transparent;
        border: 1px solid rgba(47, 49, 75, 1);
        border-radius: 5px;

        .van-field__control {
          color: #8b84e3;
          &::-webkit-input-placeholder {
            color: #8b84e3;
          }
        }
      }
    }
  }

  .circle-btm {
    height: auto;
    padding-top: 5px;
    padding-bottom: 5px;
    box-sizing: border-box;

    .btm-btnbox {
      border: none;
      justify-content: space-between;
      padding-bottom: 5px;
      box-sizing: border-box;

      .li {
        flex: initial;
        border-right: none;

        &:nth-child(1) {
          width: 33px;
          height: 29px;
          border: 0.5px solid rgba(126, 119, 210, 1);
          border-radius: 5px;
          margin-right: 5px;
          box-sizing: border-box;
        }

        &:nth-child(n + 2) {
          width: 43px;
          height: 43px;
          border-radius: 50%;
          color: #1921ec;
          font-family:
            PingFang SC,
            serif;
          font-size: 13px;
          font-weight: 500;
          line-height: 18px;
          text-align: center;
          background: url('@/assets/img/circle-select-money.png') no-repeat;
          background-size: 100% 100%;

          span {
            transform: scale(0.8);
          }
        }
      }

      .li.activeInvestMoney {
        background: url('@/assets/img/circle-select-money.png') no-repeat;
        background-size: 100% 100%;
        color: #1921ec;
        position: relative;
        transform: translateY(-5px);

        &:after {
          content: '';
          display: block;
          width: 18px;
          height: 2px;
          border-radius: 2px;
          background-color: #6e62fe;
          position: absolute;
          bottom: -6px;
          left: 50%;
          transform: translateX(-50%);
        }
      }
    }

    .text {
      margin-left: 10px;
      box-sizing: border-box;
    }
  }

  .balance {
    padding-left: 10px;
    padding-top: 4px;
    text-align: left;

    span:nth-child(1) {
      font-size: 12px;
      color: #5e6994;
    }

    span:nth-child(2) {
      font-size: 12px;
      color: #424ddb;
    }
  }

  .btm-btn {
    color: #fff;
    margin: 0 10px;
    padding-top: 8px;
    font-size: 11px;
    justify-content: flex-end;
    border-top: 1px solid rgba(50, 51, 73, 1);

    .btn-box {
      width: 100%;
      display: flex;
      justify-content: space-between;

      .icon {
        display: block;
        width: 17px;
        height: 17px;
        margin-right: 4px;
      }

      .cancel {
        background: url('@/assets/img/bet/cancel_icon.png') center center no-repeat;
        background-size: contain;
      }

      .clear {
        background: url('@/assets/img/bet/clear_icon.png') center center no-repeat;
        background-size: contain;
      }

      .btn-bg {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 86px;
        height: 45px;
        line-height: 1;
        color: #fff;
        font-size: 14px;
        box-sizing: border-box;
        padding-bottom: 2px;
        background: url('@/assets/img/bet/cancel_bg.png') center center no-repeat;
        background-size: contain;

        &.active {
          background: url('@/assets/img/bet/clear_bg.png') center center no-repeat;
          background-size: contain;
        }
      }

      .bet-btn {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 172px;
        height: 47px;
        background: url('@/assets/img/bet/bet_bg.png') center center no-repeat;
        background-size: contain;
        box-sizing: border-box;
        padding-bottom: 2px;
        line-height: 1;
      }

      .main-text {
        font-size: 16px;
        margin-bottom: 2px;
      }
    }
  }

  .all-cancel-btn {
    background: linear-gradient(180deg, #ff786f 0%, #ff2520 100%);
    box-shadow:
      0 2px 5px 0 #ff2f2f80,
      0 1px 0 0 #ff7470;
    width: 83px;
    height: 37px;
    color: white;
    font-size: 15px;
    border-radius: 5px;
  }

  .disable-all-cancel-btn {
    background: linear-gradient(180deg, #e5ebf1 0%, #a0a9c3 100%);
    box-shadow:
      0 2px 5px 0 #a0a9c3,
      0 1px 0 0 #a0a9c3;
  }
}
</style>
